using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SalesAutomationDAL
{
    public class CQueryDataDAL
    {
        private static readonly string CONN_STR = "";
        private string _strProcedureName = string.Empty;

        private bool _blnIsCustomeXML = false;
        private string _title = "";

        public bool IsCustomXML
        {
            get
            {
                return _blnIsCustomeXML;
            }
        }

        public string Title
        {
            get
            {
                return _title;
            }
        }

        static CQueryDataDAL()
        {
            CONN_STR = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SASConnStr"].ConnectionString;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public DataSet GetQueryData(string strQueryList, CommandType cmdType)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd;
            string strKeyIndex = string.Empty;
            string strValueIndex = string.Empty;

            using (SqlConnection conn = new SqlConnection(CONN_STR))
            {
                if (cmdType == CommandType.Text)
                    cmd = new SqlCommand("select * from " + strQueryList, conn);
                else
                {
                    cmd = new SqlCommand();
                    // application=search&for=accounts&name=yahoo
                    foreach (string str in strQueryList.Split('&'))
                    {
                        strKeyIndex = str.Split('=')[0].ToString();
                        strValueIndex = System.Web.HttpUtility.UrlDecode(str.Split('=')[1].ToString());

                        if (strKeyIndex.ToLower() == System.Configuration.ConfigurationSettings.AppSettings["OperationKey"].ToLower())
                        {
                            cmd.Connection = conn;
                            GetProcedureName(strValueIndex);
                            cmd.CommandText = _strProcedureName;
                        }
                        else
                            cmd.Parameters.Add(new SqlParameter("@" + strKeyIndex, strValueIndex));
                    }

                }
                cmd.CommandType = cmdType;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    da.Fill(ds);

                    return ds;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strOperationName"></param>
        /// <returns></returns>
        private void GetProcedureName(string strOperationName)
        {
            // TODO: Change code here for parsing XML
            DataSet ds = new DataSet();
            try
            {

                ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("OperationMapping.XML"));
                DataRow[] dr = ds.Tables[1].Select("Name='" + strOperationName + "'");
                if (dr.Length > 0)
                {
                    _strProcedureName = dr[0]["SP"].ToString();
                    if (dr[0]["Type"].ToString() == "0")
                        _blnIsCustomeXML = true;

                    _title = dr[0]["Title"].ToString();
                
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
